package com.wipz.eoa.iphone.myworkmanagent.mywork;

import java.util.HashMap;
import java.util.Map;

import com.toft.core2.ToftException;
import com.toft.widgets.workflow.model.CompleteTaskResult;
import com.toft.widgets.workflow.utils.ConstantUtil;
import com.wipz.eoa.iphone.businessProcess.BusinessCoreServices;

public class WorkEntrustServiceImpl extends BusinessCoreServices implements
		IWorkEntrustService {
	public Map bxSubmit(Map param) {
		String userCode = (String) param.get("COMMITERCODE");
		//获取ID
		String id = (String)param.get("ID"); 
		//任务唯一标识taskId
		String taskId = (String) param.get("taskId");
		//处理意见
		String commentContent = (String)param.get("comment");
		//流向
		String transition = (String) param.get("transition");
		//处理类型
		String typeaccept = (String) param.get("pass");
		
		String uuitId = (String) param.get("uuitId");
		
		boolean pass = "true".equals(typeaccept) ? true:false;
		Map variable = new HashMap();
		variable.put(ConstantUtil.TASK_COMMNET_TYPE, ConstantUtil.APPROVE_OPERATION_PASS.equals(typeaccept) ? ConstantUtil.TASK_COMMNET_TYPE_ACCEPT : ConstantUtil.TASK_COMMNET_TYPE_REFUSE); // 意见类型
		variable.put(ConstantUtil.TASK_COMMENT_CONTENT, commentContent); // 意见内容
		CompleteTaskResult result = this.complete(userCode, taskId, transition, variable, pass,uuitId);
		if(result == null) {
			return null;
		}
		String process_status="1"; //本条单据默认的状态是审批中
		if(result.isEnded()){
			process_status="2";//审批完成
			setWorkflowStaus(id, process_status);
		}else if(result.isBack()){
			process_status="4";//驳回
			setWorkflowStaus(id, process_status);
		}
		String nextOne = result.getNextOperators();
		Map map = new HashMap();
		if("".equals(nextOne)||result.isEnded())
			nextOne = "审批结束";
		map.put("NEXTONE", nextOne);
		map.put("ID", id);
		super.update("mywork.nextOne", map);
		
		Map resultMap = new HashMap();
		resultMap.put("back", result.isBack()+"");
		resultMap.put("ended",result.isEnded()+"");
		resultMap.put("nextOperators",result.getNextOperators());
		return resultMap;
		//this.getContext().addAttribute("result", result);
	}
	
	/**
	 * Set the status of process in each phase.
	 * @param businessRecordId 业务Id编号
	 * @param status 审批状态
	 * @throws ToftException
	 */
	public void setWorkflowStaus(String businessRecordId,String status){
		Map map = new HashMap();
		map.put("ID",businessRecordId);
		map.put("PROCESSSTAT", status);
		super.update("mywork.updateProcessStatusById",map);
	}

	
}
